package com.macaline.controller.menu;

import com.macaline.bean.Result;
import com.macaline.entity.SysMenu;
import com.macaline.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/sysMenu")
public class SysMenuControler {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 删除菜单信息
     * @param id
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id, HttpServletRequest request){
        sysMenuService.delete(id);
        return Result.success();
    }


    /**
     * 获取菜单列表
     * @return
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request){
        List<SysMenu> sysMenus = sysMenuService.selectList();
        request.setAttribute("list", sysMenus);
        return "sys/menu/list";
    }

    /**
     * 菜单添加页面
     * @param request
     * @return
     */
    @RequestMapping("/addP")
    public String addP(HttpServletRequest request){
        return "sys/menu/add";
    }

    /**
     * 添加菜单
     * @param sysMenu 菜单信息
     * @return
     */
    @ResponseBody
    @RequestMapping("/add")
    public Result add(HttpServletRequest request, SysMenu sysMenu){
        SysMenu insert = sysMenuService.insert(sysMenu);
        if(insert == null){
            return Result.error();
        }
        return Result.success();
    }

}
